package game;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import java.util.LinkedList;

public class BaseEnemy extends AnimatedSprite
{
	private static final long serialVersionUID = 1L;
	boolean isDead = false;
	private float HP;
	
	boolean isBomber = false;
	
	private float oldHP;
	private float time;
	BloodBar hpBar;
	private boolean mostrarHud = false;
	boolean isEnemy3 = false;
	
	protected float speedShoot;
	protected Point2D speed;
	
	public static LinkedList<Mine> enemyMine = new LinkedList<Mine>();
	
	public void addHP(float hP) {
		HP += hP;

	}
	
	public BaseEnemy(int indexImage, Point position, Point size, Point sPos, Point recorte, Point rowLine, float time, boolean input, int HP, float speedShoot2)
	{
		super(indexImage, position, size, sPos, recorte, rowLine, time, input);
		hpBar = new BloodBar(26,new Point((int)position.getX(), (int)position.getY()),this.dimension,HP);
		this.speedShoot = speedShoot2;
		this.speed = new Point2D.Double(0, 0);
		this.HP = HP;
		enemyMine = new LinkedList<Mine>();
	}
	
	@Override
	public void Update() {
		super.Update();
	}

	public void Update(Player p)
	{	
		Update();
		
		if(this.animationType.getY() == 1)
			speed = new Point2D.Double(0, speedShoot);
		if(this.animationType.getY() == 2)
			speed = new Point2D.Double(-speedShoot, 0);
		if(this.animationType.getY() == 3)
			speed = new Point2D.Double(0, -speedShoot);
		if(this.animationType.getY() == 4)
			speed = new Point2D.Double(speedShoot, 0);
		
		time += Time.getInstance().getLoopTime();
		
		if(oldHP != HP)
		{
			oldHP = HP;
			time = 0;
			this.mostrarHud = true;	
		}
		if(time > 2)
		{
			this.mostrarHud = false;
		}
		if(this.getRectangle().intersects(new Rectangle((int)Player.pos.getX() - 100, (int)Player.pos.getY() - 100, 250, 250)))
		{
			time = 0;
			this.mostrarHud = true;
		}

		hpBar.Update();
		hpBar.UpdateBar(this.HP, new Point((int)position.getX(), (int)position.getY()),this.dimension.y);
		
		if(HP <= 0)
		{
			isDead = true;
		}
		
		if(isDead == false)
		{
			for(int x = 0; x < Explosao.getExplosaoNormal().size(); x++)
				if(Explosao.getExplosaoNormal().get(x).getRectangle().intersects(this.getRectangle()))
				{
					isDead = true;
					break;
				}
		}
		if(isDead)
			Explosao.IniciarExplosao(new Rectangle((int)this.getRectangle().getMinX() + 5, (int)this.getRectangle().getMinY() + 5,
													(int)this.getRectangle().getWidth() - 10, (int)this.getRectangle().getHeight() - 10 ), 0.02f);
		
		
		if(isBomber)
		{
			if(ColisaoRect(p.getRectangle()))
			{
					this.isDead = true;
					Explosao.IniciarExplosaoRed(new Rectangle((int)this.getRectangle().getMinX() + 5, (int)this.getRectangle().getMinY() + 5,
							(int)this.getRectangle().getWidth() - 10, (int)this.getRectangle().getHeight() - 10 ), 0.02f);
					
				if(isEnemy3 == false)
					Player.addHP(-7);
				else
					Player.addHP(-14);
			}
		}
	}	
	public boolean IsDead()
	{
		return isDead;
	
	}
	
	public void SetDead(boolean status)
	{
		isDead = status;
	}
	
	public void setBomber(boolean status)
	{
		this.isBomber = status;
	}
	public boolean getBomber()
	{
		return this.isBomber;
	}
	
	public Point InstanciarPosTiro(Rectangle pos)
	{
		Point p3 = new Point(0,0);
		
		switch(animationType.y)
		{
			case 1:
				p3 = new Point(pos.x + 15, (int)pos.getMaxY() - 5);
				break;
			case 2:
				p3 = new Point((int)pos.getMinX() - 5, this.getRectangle().y + 15);
				break;
			case 3:
				p3 = new Point(pos.x + 15, (int)pos.getMinY() - 5);
				break;
			case 4:
				p3 = new Point((int)pos.getMaxX() - 5, pos.y + 15);
				break;
		}
		
		return p3;
	}
	
	@Override
	public void draw(Graphics2D g2d) 
	{
		super.draw(g2d);
		/*
		g2d.drawRect(this.getRectangle().x,
				this.getRectangle().y,
				this.getRectangle().width,
				this.getRectangle().height);
		
		*/
		for(Mine m : enemyMine)
		{
			m.draw(g2d);
		}
		
		if(this.mostrarHud)
			hpBar.draw(g2d);
	}
	
}